package com.woniuxy.carlife.carlifeuserweb.controller;

import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.JwtUtil;
import com.woniuxy.carlife.carlifeuserweb.util.MinioTemplate;
import com.woniuxy.carlife.carlifeuserweb.util.StatusMessageEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author zjq
 * @ClassName:MinioController
 * @Description:文件上传
 * @date 2021/4/12 17:22
 **/

@RestController
@Api(tags = "文件上传")//用于描述接口类的相关信息，作用于类上
@RequestMapping("/maintenance")
public class MinioController {

    @Resource
    private MinioTemplate mt;


    @ApiOperation(value = "用户上传头像")

    @PostMapping("/upload")
    public String upload(@ModelAttribute MultipartFile file) {

        ServletRequestAttributes sra =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        if (claims == null) {

            throw new RuntimeException("上传失败");
        }
        Object o = claims.get("token");
        String tokenUserId = String.valueOf(o);
        try {
            LocalDate ld = LocalDate.now();
            StringBuffer fileName = new StringBuffer("/");
        fileName.append(ld.getYear()).append("/")
        .append(ld.getMonthValue()).append("/")
                .append(ld.getDayOfMonth()).append("/")
                .append(System.currentTimeMillis());
            fileName.append("user_info_image").append("/")
                    .append("userId").append("/")
                    .append(tokenUserId);
//                        .append("1");
            String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            fileName.append(type);

            System.out.println(mt.uploadFile(fileName.toString(), "userinfo", file.getInputStream(), file.getSize(), file.getContentType()));

            return mt.uploadFile(fileName.toString(), "userinfo", file.getInputStream(), file.getSize(), file.getContentType());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败");
        }
    }

}
